package com.skyable.account.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.skyable.account.entity.SysDept;
import com.skyable.account.mapper.DeptMapper;
import com.skyable.account.service.IDeptService;
import com.skyable.common.common.ServiceResult;
import com.skyable.common.constants.exception.AccountExcpConstants;
import com.skyable.common.constants.param.CommonParams;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author GaoYuan
 * @title: DeptServiceImpl
 * @projectName spring-cloud-oauth2-demo
 * @description: TODO
 * @date 2021/4/1713:59
 */
@Service
public class DeptServiceImpl extends ServiceImpl<DeptMapper,SysDept> implements IDeptService {

    @Override
    public ServiceResult<SysDept> insertDept(SysDept sysDept) {
        ServiceResult<SysDept> serviceResult = new ServiceResult<>();
        SysDept dept = lambdaQuery().eq(SysDept::getDeptCode, sysDept.getDeptCode()).one();
        if(dept != null) {
            serviceResult.setError(AccountExcpConstants.ERROR_CODE_DEPT_EXISTS,AccountExcpConstants.ERROR_MSG_DEPT_EXISTS);
            return serviceResult;
        }
        sysDept.setStatus(0);
        sysDept.setDelFlag(0);
        if(sysDept.getParentId() != 0) {
            Long parentId = sysDept.getParentId();
            SysDept parentDept = baseMapper.selectById(parentId);
            sysDept.setAncestors(parentDept.getAncestors() + "|" + parentDept.getDeptCode());
        } else {
            sysDept.setAncestors("");
        }

        baseMapper.insert(sysDept);
        serviceResult.setResult(sysDept);
        return serviceResult;
    }

    @Override
    public ServiceResult<SysDept> updateDept(SysDept sysDept) {
        ServiceResult<SysDept> serviceResult = new ServiceResult<>();
        SysDept dept = lambdaQuery().ne(SysDept::getId,sysDept.getId()).eq(SysDept::getDeptCode, sysDept.getDeptCode()).one();
        if(dept != null) {
            serviceResult.setError(AccountExcpConstants.ERROR_CODE_DEPT_EXISTS,AccountExcpConstants.ERROR_MSG_DEPT_EXISTS);
            return serviceResult;
        }
        baseMapper.updateById(sysDept);
        serviceResult.setResult(sysDept);
        return serviceResult;
    }

    @Override
    public ServiceResult<IPage<SysDept>> findDeptListByPage(int page, int limit, SysDept sysDept) {
        ServiceResult<IPage<SysDept>> serviceResult = new ServiceResult<>();
        Page<SysDept> sysDeptPage = new Page<>(page, limit);
        QueryWrapper<SysDept> queryWrapper = new QueryWrapper<>();
        //需要查询未停用的部门
        sysDept.setStatus(0);
        queryWrapper.setEntity(sysDept);
        queryWrapper.orderByAsc(CommonParams.ORDER_CREATE_TIME);
        IPage<SysDept> deptIPage = baseMapper.selectPage(sysDeptPage, queryWrapper);
        serviceResult.setResult(deptIPage);
        return serviceResult;
    }

    @Override
    public ServiceResult<List<SysDept>> findDeptList() {
        ServiceResult<List<SysDept>> serviceResult = new ServiceResult<>();
        List<SysDept> sysDeptList = lambdaQuery().eq(SysDept::getStatus,0).orderByAsc(SysDept::getOrderNum).list();
        serviceResult.setResult(sysDeptList);
        return serviceResult;
    }

    @Override
    public ServiceResult<SysDept> findDeptById(Long deptId) {
        ServiceResult<SysDept> serviceResult = new ServiceResult<>();
        SysDept sysDept = lambdaQuery().eq(SysDept::getId, deptId).one();
        serviceResult.setResult(sysDept);
        return serviceResult;
    }

    @Override
    public ServiceResult<Integer> deleteDeptById(Long deptId) {
        ServiceResult<Integer> serviceResult = new ServiceResult<>();
        int result = baseMapper.deleteById(deptId);
        serviceResult.setResult(result);
        return serviceResult;
    }
}
